﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using vtech_formBase;
using vtech_frameWorks.Exceptions;

namespace vtech_system.Role
{
    public partial class frmEditRole : frmEdit
    {
        #region Delegate
        public delegate void EditDelegate();
        public event EditDelegate OnEdit = null;

        protected override void FireEvent()
        {
            if (this.OnEdit != null)
            {
                this.OnEdit();
            }
        }

        #endregion

        #region Constructor
        public frmEditRole()
        {
            this.InitializeComponent();
        }

        public frmEditRole(Guid ID)
        {
            this.InitializeComponent();
            this.ID = ID;
        }

        #endregion

        protected override bool CheckControl()
        {
            bool flag = true;
            try
            {
                this.errorProvider.Dispose();
                if (this.txtCode.Text == "")
                {
                    this.errorProvider.SetError(this.txtCode, "Bạn chưa nhập mã quyền. Xin vui lòng kiểm tra lại");
                    flag = false;
                }
                else
                {
                    if (this.txtName.Text == "")
                    {
                        this.errorProvider.SetError(this.txtName, "Bạn chưa nhập tên quyền. Xin vui lòng kiểm tra lại");
                        flag = false;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                flag = false;
            }
            return flag;
        }

        protected override void GetData()
        {
            try
            {
                ClsRoleBS obj = new ClsRoleBS();
                DataRow[] drow;
                drow = obj.GetAll().Select(string.Format("ID='{0}'", this.ID));
                foreach (DataRow row in drow)
                {
                    this.txtCode.Text = row["Code"].ToString();
                    this.txtName.Text = row["Name"].ToString();
                    this.txtDescription.Text = row["Description"].ToString();

                    if (row["Inactive"].ToString().Equals("True"))
                    {
                        this.chkActive.Checked = true;
                    }
                    else
                    {
                        this.chkActive.Checked = false;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        protected override void Edit()
        {
            try
            {
                using (DsRole dsRole = new DsRole())
                {
                    ClsRoleBS obj = new ClsRoleBS();
                    DataRow drow = dsRole.DtRole.NewRow();
                    dsRole.DtRole.Rows.Add(drow);
                    drow["Code"] = this.txtCode.Text;
                    drow["Name"] = this.txtName.Text;
                    drow["Description"] = this.txtDescription.Text;
                    if (this.chkActive.Checked == true)
                    {
                        drow["Inactive"] = 1;
                    }
                    else
                    {
                        drow["Inactive"] = 0;
                    }
                    obj.DoEdit(dsRole.DtRole);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
    }
}